import store from '@/store'
import request from '@/utils/request'
/* 注册 */
export const registerAPI = ({ username, password, repassword }) => {
  return request({
    url: '/api/reg',
    method: 'post',
    data: { username, password, repassword }
  })
}
/* 登录 */
export const loginAPI = ({ username, password }) => {
  return request({
    url: '/api/login',
    method: 'post',
    data: { username, password }
  })
}
/* 获取用户基本信息 */
export const getUserInfoAPI = () => {
  return request({
    url: '/my/userinfo',
    method: 'Get',
    headers: { Authorization: store.state.token }
  })
}
/* 获取侧边栏数据 */
export const getMenusAPI = () => {
  return request({
    url: '/my/menus',
    method: 'Get',
    headers: { Authorization: store.state.token }
  })
}
/* 修改用户信息 */
export const updateUserInfoAPI = ({ id, email, nickname, user_pic, username }) => {
  return request({
    url: '/my/userinfo',
    method: 'PUT',
    data: {
      id,
      email,
      nickname,
      user_pic,
      username
    }
  })
}
/* 更换用户头像 */
export const updateAvatarAPI = (avatar) => {
  return request({
    url: '/my/update/avatar',
    method: 'PATCH',
    data: { avatar }
  })
}
/* 修改密码 */
export const updatePwdAPI = ({ old_pwd, new_pwd, re_pwd }) => {
  return request({
    url: '/my/updatepwd',
    method: 'PATCH',
    data: { old_pwd, new_pwd, re_pwd }
  })
}
/* 获取文章分类 */
export const getArtCateListAPI = () => {
  return request({
    url: '/my/cate/list',
    method: 'Get'
  })
}
/* 添加文字分类 */
export const addArtCateAPI = ({ cate_name, cate_alias }) => {
  return request({
    url: '/my/cate/add',
    method: 'POST',
    data: { cate_name, cate_alias }
  })
}
/* 修改文章 */
export const updateArtCateAPI = ({ id, cate_name, cate_alias }) => {
  return request({
    url: '/my/cate/info',
    method: 'PUT',
    data: { id, cate_name, cate_alias }
  })
}
/* 删除文章 */
export const delArtCateAPI = (id) => {
  return request({
    url: '/my/cate/del',
    method: 'DELETE',
    params: {
      id
    }
  })
}
/* 获取文章列表 */
export const getArticleListAPI = ({ pagenum, pagesize, cate_id, state }) => {
  return request({
    url: '/my/article/list',
    params: {
      pagenum,
      pagesize,
      cate_id,
      state
    }
  })
}
/* 发布文章 */
export const uploadArticleAPI = (fd) => {
  return request({
    url: '/my/article/add',
    method: 'POST',
    data: fd// 参数要的是表单对象,不能写表单对象,axios内部会判断,如果是表单对象,传递的请求体会设置content-Type:form-data与后端对应
  })
}
/* 获取文章详情 */
export const getArticleDetailFn = (id) => {
  return request({
    url: '/my/article/info',
    method: 'Get',
    params: {
      id
    }
  })
}
/* 删除文章 */
export const delArticleAPI = (id) => {
  return request({
    url: '/my/article/info',
    method: 'DELETE',
    params: { id }
  })
}
